<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="应用名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入应用名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="appUpdateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" align="center"/>
      <el-table-column label="AppID" width="150" align="center" prop="appId"/>
      <el-table-column label="应用名称" :show-overflow-tooltip="true" width="150" align="center" prop="name"/>
      <el-table-column label="更新标题" :show-overflow-tooltip="true" align="center" prop="title"/>
      <el-table-column label="更新内容" :show-overflow-tooltip="true" align="center" prop="content"/>
      <el-table-column label="平台" width="60" align="center" prop="platform"/>
      <el-table-column label="版本号" width="60" align="center" prop="versionNumber"/>
      <el-table-column label="版本code" width="80" align="center" prop="autoIncrementVersionNumber"/>
      <el-table-column label="存储路径" :show-overflow-tooltip="true" align="center" prop="storagePath"/>
      <el-table-column label="下载链接" :show-overflow-tooltip="true" align="center" prop="downloadUrl"/>
      <el-table-column label="上传时间" align="center" prop="uploadTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.uploadTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" fixed="right" width="180px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改app更新对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="应用名称" prop="appId">
          <el-select @change="selectApp" ref="selectLabel" clearable filterable v-model="form.appId"
                     placeholder="请选择">
            <el-option v-for="item in appListOption"
                       :label="item.name"
                       :key="item.appId"
                       :value="item.appId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="更新标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入更新标题"/>
        </el-form-item>
        <el-form-item label="更新内容">
          <el-input v-model="form.content" type="textarea" placeholder="请输入更新内容"/>
        </el-form-item>
        <el-form-item label="平台" prop="platform">
          <el-input v-model="form.platform" placeholder="请输入平台"/>
        </el-form-item>
        <el-form-item label="版本号" prop="versionNumber">
          <el-input v-model="form.versionNumber" placeholder="请输入版本号"/>
        </el-form-item>
        <el-form-item label="版本code" prop="versionNumber">
          <el-input v-model="form.autoIncrementVersionNumber" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="下载链接" prop="downloadUrl">
          <el-input v-model="form.downloadUrl" type="textarea" placeholder="请输入下载链接"/>
          <file-upload v-model="form.downloadUrl" :fileSize=150 :fileType="['apk']"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAppUpdate,
  getAppUpdate,
  delAppUpdate,
  addAppUpdate,
  updateAppUpdate,
  getLatestVersion
} from "@/api/eye/appUpdate";
import {getAppListOption} from "@/api/eye/appList";

export default {
  name: "AppUpdate",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // app更新表格数据
      appUpdateList: [],
      // 应用列表下拉框
      appListOption: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        appId: null,
        name: null,
        title: null,
        content: null,
        platform: null,
        versionNumber: null,
        autoIncrementVersionNumber: null,
        downloadUrl: null,
        uploadTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "应用名称不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    selectApp(appid) {
      if (appid !== undefined && appid !== "") {
        getLatestVersion({appid: appid}).then(res => {
          if (res.data) {
            this.form.autoIncrementVersionNumber = res.data.autoIncrementVersionNumber + 1
            let v = res.data.versionNumber
            console.log(v)
            let lastIndex = v.lastIndexOf(".")
            let length = v.length
            this.form.versionNumber = v.substr(0, lastIndex + 1) + (parseInt(v.substr(lastIndex + 1, length - 1)) + 1)
          } else {
            this.form.version = "1.0.0"
            this.form.autoIncrementVersionNumber = 10
          }
        })
      }
    },
    /** 查询app更新列表 */
    getList() {
      this.loading = true;
      listAppUpdate(this.queryParams).then(response => {
        this.appUpdateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //查询应用列表下拉框
    getAppListOption() {
      getAppListOption().then((res) => {
        this.appListOption = res.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        appId: null,
        name: null,
        title: null,
        content: null,
        platform: null,
        versionNumber: null,
        downloadUrl: null,
        uploadTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getAppListOption()
      this.open = true;
      this.title = "添加app更新";
      this.form.title = "部分优化";
      this.form.content = "部分优化";
      this.form.platform = "安卓";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAppUpdate(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改app更新";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        console.log(this.$refs.selectLabel.selected.label);
        this.form.name = this.$refs.selectLabel.selected.label
        if (valid) {
          if (this.form.id != null) {
            updateAppUpdate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppUpdate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function () {
        return delAppUpdate(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('update/eye/export', {
        ...this.queryParams
      }, `eye_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
